﻿using System;
using System.Windows;
using ControleRepresentanteSync.Application;
using ControleRepresentanteSync.View;

namespace ControleRepresentanteSync
{
    internal class SectionItem : ViewModelBase, ISectionItem
    {
        public event EventHandler Open;

        public static readonly DependencyProperty TitleProperty = 
            DependencyProperty.Register("Title", typeof (string), typeof (SectionItem), new PropertyMetadata(OnTitleChanged));

        private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SectionItem) d).FireTitleChanged();
        }

        public event EventHandler TitleChanged;
        public event EventHandler IsCheckedChanged;

        private void FireTitleChanged()
        {
            if (TitleChanged != null)
                TitleChanged(this, EventArgs.Empty);
        }

        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool?), typeof(SectionItem), new PropertyMetadata(OnIsCheckedChanged));

        private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SectionItem) d).FireIsCheckedChanged();
        }

        private void FireIsCheckedChanged()
        {
            if (IsCheckedChanged != null)
                IsCheckedChanged(this, EventArgs.Empty);
        }

        public string Title
        {
            get { return (string) GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public bool? IsChecked
        {
            get { return (bool?) GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        public void FireOpen()
        {
            if (Open != null)
                Open(this, EventArgs.Empty);
        }
    }
}